/**
 * @Description : 用户模块
 **/
const userModel = require("../model/userModel");
const { output } = require("../common/index");
const qs = require("qs");
const moment = require("moment");

//用户注册
const register = async (req, res) => {
    try {
        var tmpRequest = req.body ? qs.parse(req.body) : {}; //获取表单或参数内容
        if (tmpRequest && tmpRequest.account) {
            var tmpFlag = false;
            await userModel
                .aggregate([{ $match: { account: tmpRequest.account, status: 1 } }, { $count: "count" }])
                .then((result) => {
                    if (result && result[0] && result[0].count > 0) {
                        output(res, { code: 2, msg: tmpRequest.account + " 账号已存在", data: {} });
                    } else {
                        tmpFlag = true;
                    }
                })
                .catch((err) => {
                    output(res, { code: 2, msg: "查询数据数量操作错误" + JSON.stringify(err) });
                });

            if (tmpFlag) {
                var insertArr = {
                    account: tmpRequest.account, //登录账号
                    password: tmpRequest.password, //登录密码
                    nick_name: tmpRequest.nick_name, //昵称
                    create_user: tmpRequest.account, //创建人
                    create_time: moment().format("YYYY-MM-DD HH:mm:ss") //创建时间
                };
                await userModel
                    .create(insertArr)
                    .then((result) => {
                        output(res, { code: 1, msg: "注册账号成功", data: {} });
                    })
                    .catch((err) => {
                        output(res, { code: 2, msg: "注册数据操作错误" + JSON.stringify(err) });
                    });
            }
        } else {
            output(res, { code: 2, msg: "非法参数", data: {} });
        }
    } catch (error) {
        output(res, { code: 2, msg: "操作错误" + JSON.stringify(error) });
    }
};

//用户登录
const login = async (req, res) => {
    try {
        var tmpRequest = req.body ? qs.parse(req.body) : {}; //获取表单或参数内容
        if (tmpRequest && tmpRequest.account) {
            var tmpFlag = false;
            await userModel
                .aggregate([{ $match: { account: tmpRequest.account, status: 1 } }, { $count: "count" }])
                .then((result) => {
                    if (result && result[0] && result[0].count > 0) {
                        tmpFlag = true;
                    } else {
                        output(res, { code: 2, msg: "不存在此账号数据", data: {} });
                    }
                })
                .catch((err) => {
                    output(res, { code: 2, msg: "查询数据数量操作错误" + JSON.stringify(err) });
                });

            if (tmpFlag) {
                await userModel
                    .aggregate([{ $match: { account: tmpRequest.account, status: 1 } }])
                    .then((result) => {
                        if (result[0].password != tmpRequest.password) {
                            output(res, { code: 2, msg: "登录账号密码错误", data: {} });
                        } else {
                            output(res, { code: 1, msg: "登录成功", data: { nick_name: result[0].nick_name } });
                        }
                    })
                    .catch((err) => {
                        output(res, { code: 2, msg: "查询数据操作错误" + JSON.stringify(err) });
                    });
            }
        } else {
            output(res, { code: 2, msg: "非法参数", data: {} });
        }
    } catch (error) {
        output(res, { code: 2, msg: "操作错误" + JSON.stringify(error) });
    }
};

module.exports = { register, login };
